// $codepro.audit.disable staticMemberAccess
/**
 * This class holds the Shipyard panel and all the logic behind the buttons and
 * their actions.
 * 
 * @author Araya Zaesim, Michelle Partogi, Stanley Ayzenberg, Adam Perez
 * @version 1.0
 */

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

/**
 * @author Araya Zaesim, Michelle Partogi, Stanley Ayzenberg, Adam Perez
 * @version 1.0
 */
public class Shipyard extends JPanel {

	/**
	 * Field player.
	 */
	private Player player;

	/**
	 * Field ship.
	 */
	private SpaceShip ship;

	/**
	 * Field currentFuel.
	 */
	private int currentFuel;

	/**
	 * Field maxFuel.
	 */
	private int maxFuel;

	/**
	 * Field currentCredits.
	 */
	private int currentCredits;

	/**
	 * Field fuelPrice.
	 */
	private final int fuelPrice;

	/**
	 * Field warningMessage.
	 */
	private final JOptionPane warningMessage;

	/**
	 * Field lblShipUpgrades.
	 */
	private final JLabel lblShipUpgrades;

	/**
	 * Field lblFuelingStation.
	 */
	private final JLabel lblFuelingStation;

	/**
	 * Field lblYouCurrentlyHave.
	 */
	private final JLabel lblYouCurrentlyHave;

	/**
	 * Field lblOneTankCosts.
	 */
	private final JLabel lblOneTankCosts;

	/**
	 * Field btnEquipment.
	 */
	private final JButton btnEquipment;

	/**
	 * Field btnPurchaseOneTank.
	 */
	private final JButton btnPurchaseOneTank;

	/**
	 * Field btnPurchaseShip.
	 */
	private final JButton btnPurchaseShip;

	/**
	 * Field btnRefuel.
	 */
	private final JButton btnRefuel;

	/**
	 * Field currentGui.
	 */
	private final SpaceTraderGui currentGui;

	/**
	 * Field shortRangeChart.
	 */
	private ShortRangeChart shortRangeChart;
	
	/**
	 * Constant for fuel price.
	 */
	private static final int FUEL_PRICE = 15;
	
	/**
	 * Font size.
	 */
	private static final int FONT_SIZE = 16;

	/**
	 * Constructor for the Shipyard.
	 * 
	 * @param gui
	 *            The gui that the shipyard is contained in.
	 */
	public Shipyard(SpaceTraderGui gui) {
		setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC, }));

		player = gui.getPlayer();
		ship = player.getShip();

		currentFuel = ship.getFuel();
		maxFuel = ship.getMaxFuel();
		currentCredits = player.getCredits();

		fuelPrice = FUEL_PRICE;
		warningMessage = new JOptionPane();

		currentGui = gui.getGui();
		shortRangeChart = gui.getChartPanel();

		lblShipUpgrades = new JLabel("Ship Upgrades");
		lblShipUpgrades.setFont(new Font("Tahoma", Font.BOLD, FONT_SIZE));
		add(lblShipUpgrades, "6, 4");

		lblFuelingStation = new JLabel("Fueling Station");
		lblFuelingStation.setFont(new Font("Tahoma", Font.BOLD, FONT_SIZE));
		add(lblFuelingStation, "14, 4");

		lblYouCurrentlyHave = new JLabel("You currently have " + currentFuel
				+ "/" + maxFuel + " full tanks.");
		add(lblYouCurrentlyHave, "10, 8, 9, 1, center, center");

		btnEquipment = new JButton("Equipment");
		add(btnEquipment, "6, 10");

		lblOneTankCosts = new JLabel("One tank costs " + fuelPrice
				+ " credits.");
		add(lblOneTankCosts, "14, 10");

		btnPurchaseOneTank = new JButton("Purchase One Tank");
		add(btnPurchaseOneTank, "14, 14");
		btnPurchaseOneTank.addActionListener(new ActionListener() {

			/**
			 * This method allows a player to purchase 1 tank of gas at a time.
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				final int fuelDifference = maxFuel - currentFuel;
				if (currentCredits > fuelPrice) {
					if (fuelDifference > 0) {
						ship.setFuel(currentFuel + 1);
						player.setCredits(currentCredits - fuelPrice);
						currentGui.setCredits(currentCredits - fuelPrice);
						currentGui.updateLabels();
						currentGui.setChartPanel(shortRangeChart);
					}
				}
				if (currentCredits < fuelPrice) {
					warningMessage.showMessageDialog(null,
							"You do not have enough credits!");
				}
				if (fuelDifference == 0) {
					warningMessage 
							.showMessageDialog(null, "Your tank is full!");
				}
				updatePanel();
			}
		});

		btnPurchaseShip = new JButton("Purchase Ship");
		add(btnPurchaseShip, "6, 16");

		btnRefuel = new JButton("Fill All Tanks");
		add(btnRefuel, "14, 18");
		btnRefuel.addActionListener(new ActionListener() {

			/**
			 * This allows players to fully fuel up all tanks.
			 * @param e
			 */
			public void actionPerformed(ActionEvent e) {
				final int fuelDifference = maxFuel - currentFuel;
				if (currentCredits > (fuelPrice * fuelDifference)) {
					if (fuelDifference > 0) {
						ship.setFuel(currentFuel + fuelDifference);
						player.setCredits(currentCredits
								- (fuelDifference * fuelPrice));
						currentGui.setCredits(currentCredits
								- (fuelDifference * fuelPrice));
						currentGui.updateLabels();
						currentGui.setChartPanel(shortRangeChart);
					}
				}
				if (currentCredits < (fuelPrice * fuelDifference)) {
					warningMessage.showMessageDialog(null, 
							"You do not have enough credits!");
				}
				if (fuelDifference == 0) {
					warningMessage 
							.showMessageDialog(null, "Your tank is full!");
				}
				updatePanel();
			}
		});
	}

	/**
	 * This method updates the panel to display new fuel levels after refueling.
	 */
	public void updatePanel() {
		ship = player.getShip();
		currentFuel = ship.getFuel();
		maxFuel = ship.getMaxFuel();
		currentCredits = player.getCredits();
		shortRangeChart = currentGui.getChartPanel();

		lblYouCurrentlyHave.setText("You currently have " + currentFuel + "/"
				+ maxFuel + " full tanks.");
	}

	/**
	 * A getter for the player contained in the panel.
	 * 
	
	 * @return player */
	public Player getPlayer() {
		return player;
	}

	/**
	 * A setter for the player to be contained in the panel.
	 * 
	 * @param player
	 *            The player
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}

}